<?php

class WxmenuController extends Controller
{

	/*
    * 获取列表分页
    *
    */
	public function actionGetListPaging()
	{

		try {
			//参数验证

			//检测
			$user_id = SessionService::getUserIDBySessionid();

			$wx_id = Util::getParamNotEmpty("wx_id");


			$searchsql = Util::initSearchSql();
			$searchsql = Util::getSerarchStrAdd($searchsql,"wx_id",$wx_id);


			//获取count
			$totalcount = WxMenu::model()->count($searchsql);

            //排序
            $searchsql = $searchsql . " order by sort asc,create_time desc";

            //加上分页控制
            $searchsql = Util::addLimitSql($searchsql);

            //获取list
            $list = WxMenu::model()->findAll($searchsql);

			//一级二级菜单排序
			$returnlist = array();
			foreach($list as $value){
				if($value->level == 1){
					array_push($returnlist,$value);
					foreach($list as $value2){
						if($value2->level == 2 && $value2->p_id == $value->id){
							array_push($returnlist,$value2);
						}

					}
				}
			}


            Util::responseList($totalcount, $returnlist);

        } catch (Exception $e) {
			Util::responseErrorMsg($e->getMessage());

		}


	}

	/*
	* 获取列表分页
	*
	*/
	public function actionGetpidListPaging()
	{

		try {
			//参数验证

			//检测
			$user_id = SessionService::getUserIDBySessionid();

			$wx_id = Util::getParamNotEmpty("wx_id");


			$searchsql = Util::initSearchSql();
			$searchsql = Util::getSerarchStrAdd($searchsql,"wx_id",$wx_id);
			$searchsql = Util::getSerarchStrAdd($searchsql,"p_id","-1");


			//获取count
			$totalcount = WxMenu::model()->count($searchsql);

	            //排序
	            $searchsql = $searchsql ." order by sort asc,create_time desc";

	            //加上分页控制
	            $searchsql = Util::addLimitSql($searchsql);

	            //获取list
	            $list = WxMenu::model()->findAll($searchsql);


	            Util::responseList($totalcount, $list);

	        } catch (Exception $e) {
			Util::responseErrorMsg($e->getMessage());

		}


	}

	/*
	* 添加
	*
	*/
	public function actionAdd()
	{

		try {
			//检测
			$user_id = SessionService::getUserIDBySessionid();

			//参数验证
			$wx_id = Util::getParamNotEmpty("wx_id");
			$name = Util::getParamNotEmpty("name");
			$type = Util::getParamNotEmpty("type");
			$p_id = Util::getParamNotEmpty("p_id");

			$url = Util::getParam("url","");
			$key = Util::getParam("key","");
			$sort = Util::getParam("sort","1");


			//添加
			$obj = new WxMenu();
			Util::initObjData($obj);

			if($p_id == -1){
				$level = 1;
			}else{
				$level = 2;
			}


			$obj->wx_id = $wx_id;
			$obj->name = $name;
			$obj->level = $level;
			$obj->type = $type;
			$obj->url = $url;
			$obj->key = $key;
			$obj->p_id = $p_id;
			$obj->sort = $sort;


			Util::saveObjData($obj);


			//返回数据
			Util::responseMsg(MsgConst::RETURN_SUCCESS);


		} catch (Exception $e) {
			Util::responseErrorMsg($e->getMessage());

		}
	}


	/*
	* 修改
	*
	*/
	public function actionUpdate()
	{

		try {
			//检测
			$user_id = SessionService::getUserIDBySessionid();

			//参数验证
			$obj_id = Util::getParamNotEmpty("obj_id");

			$wx_id = Util::getParamNotEmpty("wx_id");
			$name = Util::getParamNotEmpty("name");
			$type = Util::getParamNotEmpty("type");
			$p_id = Util::getParamNotEmpty("p_id");

			$url = Util::getParam("url","");
			$key = Util::getParam("key","");
			$sort = Util::getParam("sort","1");


			//验证存在
			$obj = WxMenu::model()->findByPK($obj_id);

	            Util::checkExistAndNotDelete($obj);

				if($p_id == -1){
					$level = 1;
				}else{
					$level = 2;
				}

	            //修改信息

				$obj->wx_id = $wx_id;
				$obj->name = $name;
				$obj->level = $level;
				$obj->type = $type;
				$obj->url = $url;
				$obj->key = $key;
				$obj->p_id = $p_id;
				$obj->sort = $sort;

	            //保存
	            Util::saveObjData($obj);



	            //返回数据
	            Util::responseMsg(MsgConst::RETURN_SUCCESS);


	        } catch (Exception $e) {
			Util::responseErrorMsg($e->getMessage());

		}
	}


	/*
	* 删除
	*/
	public function actionDelete()
	{

		try {
			//检测
			$user_id = SessionService::getUserIDBySessionid();

			//参数验证
			$obj_id = Util::getParamNotEmpty("obj_id");


			//删除
			$obj = WxMenu::model()->findByPK($obj_id);

	                Util::checkExistAndNotDelete($obj);

	                Util::deleteObjData($obj);

	                Util::saveObjData($obj);



	                //返回数据
	                Util::responseMsg(MsgConst::RETURN_SUCCESS);


	            } catch (Exception $e) {
			Util::responseErrorMsg($e->getMessage());

		}
	}

}